<template>
	<div class="layout-pd">
		<!-- <el-alert
			title="温馨提示：此权限页面代码及效果只作为演示使用，若出现不可逆转的bug，请尝试 `F5` 刷新页面。若实际项目中非要实现此用户权限切换功能，
      请在切换方法 `onRadioChange` 最后面添加刷新代码 `window.location.reload()`。 请注意：按钮权限页面中的演示2（指令模式）、演示3（函数模式）
      切换用户时无法动态演示，想要动态演示，请按 `F5` 或者添加 `window.location.reload()`。"
			type="warning"
			:closable="false"
		></el-alert>
		<el-alert
			:title="`当前用户页面权限：[${userInfos.roles}]，当前用户按钮权限：[${userInfos.authBtnList}]`"
			type="success"
			:closable="false"
			class="mt15"
		></el-alert> -->
		<!-- <el-card shadow="hover" header="切换用户演示，前端控制不同用户显示不同页面、按钮权限" class="mt15">
			<el-radio-group v-model="userAuth" size="default" @change="onRadioChange">
				<el-radio-button label="admin"></el-radio-button>
				<el-radio-button label="common"></el-radio-button>
			</el-radio-group>
		</el-card> -->
		<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
			<el-table-column prop="hid" label="id" width="300" />
			<el-table-column prop="name" label="作业名称"  width="300"></el-table-column>
			<!-- <el-table-column prop="roleSign" label="角色标识" show-overflow-tooltip></el-table-column>
				<el-table-column prop="sort" label="排序" show-overflow-tooltip></el-table-column> -->

			<!-- <el-table-column prop="describe" label="角色描述" show-overflow-tooltip></el-table-column> -->
			<el-table-column prop="endtime" :formatter="getLocalTime" label="截止时间"  width="400"></el-table-column>
		
			<el-table-column label="操作"  width="300">
				<template #default="scope">
					<el-button size="small" text type="primary" @click="Jump(scope)">查看</el-button>
					<!-- <el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>  -->
				</template>
			</el-table-column>
		</el-table>
	</div>
</template>

<script setup lang="ts" name="scriptfileupdouw">
import { storeToRefs } from 'pinia';
import { useUserInfo } from '/@/stores/userInfo';
import { onMounted, reactive, ref } from 'vue';
import { scriptApi } from '/@/api/script/index';
import router from '/@/router';
import { Session } from '/@/utils/storage';
import { NextLoading } from '/@/utils/loading';



// 定义变量内容
const storesUserInfo = useUserInfo();
const { userInfos } = storeToRefs(storesUserInfo);
const userAuth = ref('');

// 定义变量内容
const state = reactive<SysRoleState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			hids: [],
			search: '',
			pageNum: 1,
			pageSize: 10,
			states: [],
			getatates:[],
		},
	},
});

// 初始化用户权限
const initUserAuth = () => {
	userAuth.value = userInfos.value.roles[0];
};


// 获取发布的作业
const onRadio = async () => {
	scriptApi().getHomeWork().then(res => {
		state.tableData.data = res.data;
		for (let i = 0; i < state.tableData.data.length; i++) {
			state.tableData.param.states.push(res.data[i].hid)
			Session.set(res.data[i].hid, JSON.stringify(res.data[i].content))
		}
		//获取作业状态
		scriptApi().getState(state.tableData.param.states).then(res => {
			 localStorage.setItem('states', JSON.stringify(res.data))
		})
		state.tableData.param.getatates=JSON.parse(localStorage.getItem("states") || '{}')
	})


};


//格式化后端返回来的时间
function getLocalTime(value: any) {
	let time = value.endtime[0] + '-' + value.endtime[1] + '-' + value.endtime[2] + ' ' + value.endtime[3] + ':' + value.endtime[4] + ':' + value.endtime[5]
	return time
}



//跳转到具体页面
const Jump = (scope: any) => {
	localStorage.setItem('hid', JSON.stringify(scope.row.hid))
	router.push("/homework2")
}


// 页面加载时
onMounted(() => {
	initUserAuth()
	onRadio()
});


</script>
